package com.zyw.zhuangdian.service.impl;

import com.zyw.zhuangdian.constant.ServiceConstant;
import com.zyw.zhuangdian.dao.ApproveDao;
import com.zyw.zhuangdian.dao.CommentDao;
import com.zyw.zhuangdian.exceptions.MyDaoException;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.dto.PageDto;
import com.zyw.zhuangdian.pojo.dto.UserDto;
import com.zyw.zhuangdian.pojo.in.ApproveDtoIn;
import com.zyw.zhuangdian.pojo.in.CommentDtoIn;
import com.zyw.zhuangdian.pojo.out.CommentDtoOut;
import com.zyw.zhuangdian.service.CommentService;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
@Slf4j
public class CommentServiceImpl implements CommentService {
    private final CommentDao CommentDao;
    private final ApproveDao approveDao;

    @Override
    public Resp<Void> addComment(CommentDtoIn in) {
        Resp<Void> resp = new Resp<>();

        CommentDao.addComment(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Void> updateComment(CommentDtoIn in) {
        Resp<Void> resp = new Resp<>();

        CommentDao.updateComment(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Void> deleteComment(CommentDtoIn in) {
        Resp<Void> resp = new Resp<>();

        in.setStatus(ServiceConstant.STATUS_INVALID);

        CommentDao.updateComment(BeanConvertUtil.beanCovertMap(in));
//        级联删除点赞记录
        ApproveDtoIn approveDtoIn = new ApproveDtoIn();
        approveDtoIn.setToAid(in.getCid());
        approveDtoIn.setType("comment");
        try{
            approveDao.deleteApprove(BeanConvertUtil.beanCovertMap(approveDtoIn));
        }catch(MyDaoException e){
            log.info("没有点赞记录");
        }
        return resp;
    }

    @Override
    public Resp<List<CommentDtoOut>> listComments(CommentDtoIn in) {
        Resp<List<CommentDtoOut>> resp = new Resp<>();
        if (in.getPage() != PageDto.DEFAULT_PAGE){
            if (in.getRow() == PageDto.DEFAULT_PAGE){
                in.setPage(PageDto.DEFAULT_PAGE);
            }else{
                in.setPage((in.getPage() - 1) * in.getRow());
            }
        }
        int count = CommentDao.listCommentsCount(BeanConvertUtil.beanCovertMap(in));
        if (count > 0){
            List<Map> comments = CommentDao.listComments(BeanConvertUtil.beanCovertMap(in));
            List<CommentDtoOut> dtoOuts = new ArrayList<>();
            for (Map out:comments){
                CommentDtoOut o = BeanConvertUtil.covertBean(out,CommentDtoOut.class);
                UserDto u = BeanConvertUtil.covertBean(out,UserDto.class);
                u.harmless();
                o.setUser(u);
                dtoOuts.add(o);
            }
            resp.setData(dtoOuts);
        }

        resp.setTotal(count);
        if (in.getPage() != PageDto.DEFAULT_PAGE) {
            resp.setRecords((int) Math.ceil((double) count / (double) in.getRow()));
            resp.setPage(in.getPage());
            resp.setRow(in.getRow());
        }
        return resp;
    }
}
